package com.myfoon.servlet;


import com.myfoon.action.backing.context.MapBean;
import com.myfoon.model.entity.Location;

import com.myfoon.utils.JSONUtils;

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;


public class LocationServlet extends HttpServlet {
    public LocationServlet() {
            
    }

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException 
    {                        
        System.out.println(request.getHeader("host"));
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        String query = request.getParameter("q");
        System.out.println("QUERY ----------->" + query);
        String sllValue = request.getParameter("sll");                        
                
        MapBean mapBean = new MapBean();
        List<Location> locationList = mapBean.findLocations(query, sllValue);        
        
        JSONObject JSONConvertedLocations = new JSONUtils().convertFromLocations(locationList);                                         
                
        response.getWriter().print(JSONConvertedLocations.toString());        
        //String resultsHtml = mapBean.prepareDivHtml(locationList);
        
        //response.getWriter().write(resultsHtml);
    }

    
}
